package it.eng.area118.mdogpscollector.fragment;

import it.eng.area118.mdogpscollector.listener.FragmentActionReceiver;
import it.eng.area118.mdogpscollector.listener.ManageCheckPointListener;
import it.eng.area118.mdogpscollector.model.CheckPoint;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class CheckPointListFragment extends ListFragment {

	private static final String STATE_ACTIVATED_POSITION = "activated_position";

	private ManageCheckPointListener listener;

	private int mActivatedPosition = ListView.INVALID_POSITION;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		CheckPointAdapter adapter = new CheckPointAdapter(getActivity(), android.R.layout.simple_list_item_activated_1, android.R.id.text1, new ArrayList<CheckPoint>());
		setListAdapter(adapter);
		Log.i("CheckPointListFragment", "onCreate");

	}

	public void setWaiting(boolean showList) {
		setListShown(!showList);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		((CheckPointAdapter) getListAdapter()).setSelectItem(position);
		Object o = getListAdapter().getItem(position);
		if (o != null) {
			CheckPoint cp = (CheckPoint) o;
			listener.onCheckPointSelected(cp.getItemKey());
		}
	}

	public void setContent(List<CheckPoint> points) {
		CheckPointAdapter adapter = (CheckPointAdapter) getListAdapter();
		adapter.setContent(points);
		setWaiting(false);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof FragmentActionReceiver<?>)) {
			throw new IllegalStateException("Activity must implement fragment's callbacks.");
		}

		FragmentActionReceiver receiver = (FragmentActionReceiver) activity;
		listener = (ManageCheckPointListener) receiver.getActionListener();
		Log.i("CheckPointListFragment", "onAttach");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.i("CheckPointListFragment", "onDetach");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Log.i("CheckPointListFragment", "onCreateView");
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		Log.i("CheckPointListFragment", "onViewCreated");
		if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
			setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
		}
	}

	@Override
	public void onViewStateRestored(Bundle savedInstanceState) {
		super.onViewStateRestored(savedInstanceState);
		Log.i("CheckPointListFragment", "onViewStateRestored: " + savedInstanceState);
		// getFragmentManager().putFragment(savedInstanceState,
		// CheckPointListFragment.class.getName(), this);
		Log.i("CheckPointListFragment", "onViewStateRestored");

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i("CheckPointListFragment", "onDestroy");

	}

	public void setActivatedPosition(int position) {
		if (position == ListView.INVALID_POSITION) {
			getListView().setItemChecked(mActivatedPosition, false);
		} else {
			getListView().setItemChecked(position, true);
		}

		mActivatedPosition = position;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (mActivatedPosition != ListView.INVALID_POSITION) {
			outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
		}
	}

	public void setActivateOnItemClick(boolean activateOnItemClick) {
		getListView().setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
	}

	public void selectNext() {
		CheckPointAdapter adapter = (CheckPointAdapter) getListAdapter();
		int selection = adapter.getSelectItem();
		int next = selection + 1;
		setSelection(next);
		adapter.setSelectItem(next);
		// getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
		Log.i("checkpointlistfragment", "scroll to position: " + next);
		getListView().smoothScrollToPosition(next + 1);
	}
}
